package com.amt.configure;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * @Title: SpringBean.java 
 * @Package com.amt.configure 
 * @Description: TODO(获取普通类组件) 
 * @author 江伟  
 * @date 2021年3月月11日 下午4:43:30
 * Copyright (c) ©1994-2021 Scjydz.com All Rights Reserved.
 */
@Component
@Slf4j
public class SpringBean implements ApplicationContextAware {

	private static ApplicationContext applicationContext = null;
	
	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		if(SpringBean.applicationContext == null) {
			SpringBean.applicationContext = applicationContext;
		}
		log.info("Common class get SpringBean is loads successfully. (普通类获取SpringBean模块加载成功。)");
	}
	
	/**
	 * @description: 获取applicationContext
	 */
    public static ApplicationContext getApplicationContext() {
       return applicationContext;
    }
    
   /**
    * @description: 通过name获取 Bean
    * @param name 名称
    */
    public static Object getBean(String name){
       return getApplicationContext().getBean(name);
    }
    
   /**
    * @description: 通过class获取Bean
    * @param clazz class
    */
    public static <T> T getBean(Class<T> clazz){
       return getApplicationContext().getBean(clazz);
    }
    
    /**
     * @description: 通过name,以及Clazz返回指定的Bean
     * @param name 名称
     * @param clazz class
     */
    public static <T> T getBean(String name,Class<T> clazz){
       return getApplicationContext().getBean(name, clazz);
    }

}
